package com.kingdee.hackson.controller;

import com.kingdee.hackson.dto.lc.Response;
import com.kingdee.hackson.entity.Position;
import com.kingdee.hackson.service.IAuthService;
import com.kingdee.hackson.service.IBizDataService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/query/page/list")
public class QueryController {
    private static Logger logger = LoggerFactory.getLogger(QueryController.class);
    @Autowired
    private IBizDataService bizDataService;

    @Autowired
    private IAuthService authService;

    /**
     * 默认同步的in方式查询
     */
    @GetMapping("/default/{pageNum}/{count}")
    public Response queryBizPageList(@RequestParam Long orgId, @PathVariable int pageNum, @PathVariable int count) {
        String authCode = authService.queryAuthCodeByOrgId(orgId);
        List<Long> authIds = authService.getIdListByParentPrefixCode(authCode);
        Long dataCount = bizDataService.queryListPageCountByAuthIds(authIds);
        int startNum = (pageNum - 1) * count;
        List<Position> bizDataEntities = bizDataService.queryListPageByAuthIds(authIds, startNum, count);
        return new Response(dataCount, count, bizDataEntities);
    }

    @GetMapping("/default/count")
    public Long queryBizPageListCount(@RequestParam Long orgId) {
        String authCode = authService.queryAuthCodeByOrgId(orgId);
        List<Long> authIds = authService.getIdListByParentPrefixCode(authCode);
        Long dataCount = bizDataService.queryListPageCountByAuthIds(authIds);
        return dataCount;
    }

    /**
     * 默认同步的in方式查询，但是不差count，模拟count异步的场景
     */
    @GetMapping("/default/async/{pageNum}/{count}")
    public Response queryBizPageAsyncList(@RequestParam Long orgId, @PathVariable int pageNum, @PathVariable int count) {
        String authCode = authService.queryAuthCodeByOrgId(orgId);
        List<Long> authIds = authService.getIdListByParentPrefixCode(authCode);
        // 一般分页会有此查询
        int startNum = (pageNum - 1) * count;
        List<Position> bizDataEntities = bizDataService.queryListPageByAuthIds(authIds, startNum, count);
        return new Response(0, count, bizDataEntities);
    }

    /**
     * 采用like的方式
     */
    @GetMapping("/v1/{pageNum}/{count}")
    public Response queryBizPageListByRange(@RequestParam Long orgId, @PathVariable int pageNum, @PathVariable int count) {
        String authCode = authService.queryAuthCodeByOrgId(orgId);
        // 一般分页会有此查询
        Long dataCount = bizDataService.queryListPageCountByAuthCode(authCode);
        int startNum = (pageNum - 1) * count;
        List<Position> bizDataEntities = bizDataService.queryListPageByAuthCode(authCode, startNum, count);
        return new Response(dataCount, count, bizDataEntities);
    }

    /**
     * 采用like的方式
     */
    @GetMapping("/v1/count")
    public Long queryBizPageListByRangeCount(@RequestParam Long orgId) {
        String authCode = authService.queryAuthCodeByOrgId(orgId);
        // 一般分页会有此查询
        Long dataCount = bizDataService.queryListPageCountByAuthCode(authCode);
        return dataCount;
    }

    /**
     * 采用like的方式，不查询count
     */
    @GetMapping("/v1/async/{pageNum}/{count}")
    public Response queryBizPageListAsyncByRange(@RequestParam Long orgId, @PathVariable int pageNum, @PathVariable int count) {
        String authCode = authService.queryAuthCodeByOrgId(orgId);
        // 一般分页会有此查询
        int startNum = (pageNum - 1) * count;
        List<Position> bizDataEntities = bizDataService.queryListPageByAuthCode(authCode, startNum, count);
        return new Response(0, count, bizDataEntities);
    }

    /**
     * 利用平台的Struct编码，like
     */
    @GetMapping("/v2/{pageNum}/{count}")
    public Response queryBizPageListByStructRange(@RequestParam Long orgId, @PathVariable int pageNum, @PathVariable int count) {
        String authCode = authService.queryStructNumberByOrgId(orgId);
        Long dataCount = bizDataService.queryListPageCountByStructCode(authCode);
        // 一般分页会有此查询
        int startNum = (pageNum - 1) * count;
        List<Position> bizDataEntities = bizDataService.queryListPageByStructCode(authCode, startNum, count);
        return new Response(dataCount, count, bizDataEntities);
    }

    /**
     * 利用平台的Struct编码，like
     */
    @GetMapping("/v2/count")
    public Long queryBizPageListByStructRangeCount(@RequestParam Long orgId) {
        String authCode = authService.queryStructNumberByOrgId(orgId);
        Long dataCount = bizDataService.queryListPageCountByStructCode(authCode);
        return dataCount;
    }

    /**
     * 利用平台的Struct编码，不带带count
     */
    @GetMapping("/v2/async/{pageNum}/{count}")
    public Response queryBizPageListAsyncByStructRange(@RequestParam Long orgId, @PathVariable int pageNum, @PathVariable int count) {
        String authCode = authService.queryStructNumberByOrgId(orgId);
        // 一般分页会有此查询
        int startNum = (pageNum - 1) * count;
        List<Position> bizDataEntities = bizDataService.queryListPageByStructCode(authCode, startNum, count);
        return new Response(0, count, bizDataEntities);
    }

    /**
     * 短编码多表join
     */
    @GetMapping("/v3/{pageNum}/{count}")
    public Response queryBizPageListByMergeTable(@RequestParam Long orgId, @PathVariable int pageNum, @PathVariable int count) {
        String authCode = authService.queryAuthCodeByOrgId(orgId);
        Long dataCount = bizDataService.queryListPageCountByMergeTable(authCode);
        // 一般分页会有此查询
        int startNum = (pageNum - 1) * count;
        List<Position> bizDataEntities = bizDataService.queryListPageByMergeTable(authCode, startNum, count);
        return new Response(dataCount, count, bizDataEntities);
    }

    /**
     * 短编码多表join
     */
    @GetMapping("/v3/count")
    public Long queryBizPageListByMergeTable(@RequestParam Long orgId) {
        String authCode = authService.queryAuthCodeByOrgId(orgId);
        Long dataCount = bizDataService.queryListPageCountByMergeTable(authCode);
        return dataCount;
    }

    /**
     * 短编码多表join，不带带count
     */
    @GetMapping("/v3/async/{pageNum}/{count}")
    public Response queryBizPageListAsyncByMergeTable(@RequestParam Long orgId, @PathVariable int pageNum, @PathVariable int count) {
        String authCode = authService.queryAuthCodeByOrgId(orgId);
        // 一般分页会有此查询
        int startNum = (pageNum - 1) * count;
        List<Position> bizDataEntities = bizDataService.queryListPageByMergeTable(authCode, startNum, count);
        return new Response(0, count, bizDataEntities);
    }

    /**
     * 长编码多表join
     */
    @GetMapping("/v4/{pageNum}/{count}")
    public Response queryBizPageListByMergeTableWithStructNumber(@RequestParam Long orgId, @PathVariable int pageNum, @PathVariable int count) {
        String structNumber = authService.queryStructNumberByOrgId(orgId);
        Long dataCount = bizDataService.queryListPageByMergeTableCountWithStructNumber(structNumber);
        // 一般分页会有此查询
        int startNum = (pageNum - 1) * count;
        List<Position> bizDataEntities = bizDataService.queryListPageByMergeTableWithStructNumber(structNumber, startNum, count);
        return new Response(dataCount, count, bizDataEntities);
    }
    /**
     * 长编码多表join
     */
    @GetMapping("/v4/count")
    public Long queryBizPageListByMergeTableWithStructNumber(@RequestParam Long orgId) {
        String structNumber = authService.queryStructNumberByOrgId(orgId);
        Long dataCount = bizDataService.queryListPageByMergeTableCountWithStructNumber(structNumber);
        return dataCount;
    }

    /**
     * 长编码多表join，不带带count
     */
    @GetMapping("/v4/async/{pageNum}/{count}")
    public Response queryBizPageListAsyncByMergeTableWithStructNumber(@RequestParam Long orgId, @PathVariable int pageNum, @PathVariable int count) {
        String structNumber = authService.queryStructNumberByOrgId(orgId);
        // 一般分页会有此查询
        int startNum = (pageNum - 1) * count;
        List<Position> bizDataEntities = bizDataService.queryListPageByMergeTableWithStructNumber(structNumber, startNum, count);
        return new Response(0, count, bizDataEntities);
    }
}
